package pt.ua.helpdesk.client;

import java.util.ArrayList;

import pt.ua.helpdesk.client.FaqView.GetAsyncCallback;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class FaqAdm extends Composite {

	// 1 - Activar, 2 - Remover
	public FaqAdm(int idFaq, int op) {

		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setHeight("70");
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setStyleName("loginPanel");

		final Label lblMsg = new Label("");
		verticalPanel.add(lblMsg);
		lblMsg.setStyleName("lblSectionTitle");
		lblMsg.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final Button btnVoltar = new Button();
		verticalPanel.add(btnVoltar);
		btnVoltar.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				Default.loadComposite(new FaqList(2));
			}
		});
		btnVoltar.setStyleName("buttonVoltar");
		btnVoltar.setText("« Voltar");
		
		if(op == 1) //Activar
		{
			ServiceFaq.Util.getInstance().acceptFaq(new Integer(idFaq),new AcceptAsyncCallback(lblMsg));
		}
		else if (op == 2) // Remover
		{
			ServiceFaq.Util.getInstance().removeFaq(new Integer(idFaq),new RemoveAsyncCallback(lblMsg));		
		}
		
	}
	
	/**
	 * An asynchronous callback is the primary interface that a caller
	 * must implement to receive a response from a remote procedure call
	 * 
	 * This class is the implementation of AsyncCallback for the register
	 * user service
	 * 
	 * @author Amigos do Noddy
	 */	
	  static class AcceptAsyncCallback implements AsyncCallback {
		  Label lblMsg;
		  
		  /**
		   * Class constructor
		   * @param html Widget that can contain arbitrary HTML
		   */
		  public AcceptAsyncCallback(Label lblMsg) {
			  this.lblMsg = lblMsg;
		  }
		  
		  /**
		   * Success handler
		   */
		  public void onSuccess(Object object) {
			  Boolean ret = (Boolean)object;
			    
			  if(ret.booleanValue())
				  lblMsg.setText("FAQ aceite com Sucesso!");			  
		  }
		  
		  /**
		   * Failure handler
		   */
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
		  }
	}
	  
		/**
		 * An asynchronous callback is the primary interface that a caller
		 * must implement to receive a response from a remote procedure call
		 * 
		 * This class is the implementation of AsyncCallback for the register
		 * user service
		 * 
		 * @author Amigos do Noddy
		 */	
		  static class RemoveAsyncCallback implements AsyncCallback {
			  Label lblMsg;
			  
			  /**
			   * Class constructor
			   * @param html Widget that can contain arbitrary HTML
			   */
			  public RemoveAsyncCallback(Label lblMsg) {
				  this.lblMsg = lblMsg;
			  }
			  
			  /**
			   * Success handler
			   */
			  public void onSuccess(Object object) {
				  Boolean ret = (Boolean)object;
				    
				  if(ret.booleanValue())
					  lblMsg.setText("FAQ removido com Sucesso!");			  
			  }
			  
			  /**
			   * Failure handler
			   */
			  public void onFailure(Throwable throwable) {
				  System.out.println("FALHOU!");
			  }
		}

}
